<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 28/11/2018
 * Time: 11:30
 */

namespace App\Services\Mobile\Withdraw;

use App\Models\Finance\News;
use App\Models\Finance\Pay;
use App\Models\User\UsersInfo;
use App\Models\Agent\Agent;
use App\Models\Finance\IncomeExcel;
use DB;

class WithdrawLogic
{
    //手机端 通知 列表
    public function agentInform($data): array
    {
        $_whereOr['info_id'] = $data['info_id'];
        $_where['type'] = 1;
        $list = News::orderBy('id', 'desc')
            ->where($_where)
            ->get()
            ->toArray();
        return auto_success($list, '成功获取列表', '获取列表失败');
    }
    //代理申请提现
    public function kiting($data): array
    {
    	//搜索代理信息  
        $user = Agent::where('id','=',$data['info_id'])
            ->get()
            ->toArray();
        //1代表失败
        if ($user[0]['money'] < 100) {
        	return auto_return($user[0]['money'],'1');
        }
        $time = date('d',time());
        $times = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
        if (!in_array($time,$times)) {
            return auto_return($time,'1');
        }
        //添加用户提现信息
        $info['info_id'] = $data['info_id'];   	
        $info['name'] = $user[0]['payee_name'];   	
        $info['band_card'] = $user[0]['band_card'];   	
        $info['band_info'] = $user[0]['band_info'];   	
        $info['money'] = $user[0]['money'];   	
        $info['type'] = 1;   	  	
        $rst = Pay::insertGetId($info);
        if ($rst === false) {
            return auto_failure($info, '添加提现信息失败！');
        } elseif ($rst === 0) {
            return auto_failure($info, '未作改动！');
        }
        //减去代理提现余额
        $updateData['money'] = 0;
        $money = Agent::where('id','=',$data['info_id'])
            ->update($updateData);        
        if ($money === false) {
            return auto_failure($data, '减去代理提现余额失败！');
        } elseif ($money === 0) {
            return auto_failure($data, '未作改动！');
        }
        return auto_return(['id' => $data['info_id']], '用户已申请提现！');

    }
    //我的个人中心
    public function person($data): array
    {        
        $user = Agent::where('id','=',$data['info_id'])
            ->get()
            ->toArray();
        $_where['info_id'] = $data['info_id'];
        $_where['type'] = 1;
        $countMoney = Pay::where($_where)
    	    ->where('status','=',1)
            ->sum('money');
        $kiting = Pay::where($_where)
    	    ->where('status','=',0)
            ->sum('money');
        $res['user_name'] = $user[0]['user_name'];
        $res['address'] = $user[0]['address'];
        $res['phone'] = $user[0]['phone'];
        $res['money'] = $user[0]['money'];
        $res['sum'] = $countMoney;
        $res['kiting'] = $kiting;
        return auto_success($res, '成功获取列表', '获取列表失败');
    }
    //历史记录
    public function historyRecord($data): array
    {
        $_whereOr['info_id'] = $data['info_id'];
        $_where['type'] = 1;
        $list = Pay::orderBy('id', 'desc')
            ->where($_where)
            ->get()
            ->toArray();
        return auto_success($list, '成功获取列表', '获取列表失败');
    }
    //代理用户
    public function agencyUser($data): array
    {        
        $user = Agent::where('id','=',$data['id'])
            ->get()
            ->toArray();
        $_where['software_id'] = $user[0]['software_id'];
        $list = UsersInfo::where($_where)
            ->get()
            ->toArray();

        return auto_success($list, '成功获取列表', '获取列表失败');
    }



}